#!/bin/bash
###
# @Author: 蔡科
# @Date: 2021-05-25 16:08:38
# @LastEditTime: 2021-05-25 16:20:38
# @LastEditors: 蔡科
# @Description: In User Settings Edit
# @FilePath: /engine_run/run.sh
###

APP_NAME=gomaxai-proxy-server

dir=$(
    cd $(dirname $0)
    pwd
)

start() {

    PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

    echo "progress Pid ${PID}"

    if [[ -z "$PID" ]] ;then
            echo "start ${APP_NAME}"
            nohup ./serve.sh > nohup.output.log 2>&1 &
    else
            echo "${APP_NAME} is running"
    fi

	sleep 2

    ps -ef|grep ${APP_NAME}
}

stop() {

    #根据端口号查询对应的pid
    XII=$(ps -ef | grep serve.sh | grep -v grep | awk '{print $2}')
    PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

    echo "shell Pid ${XII}"
    echo "progress Pid ${PID}"

    if [[ -z "$PID" ]] ;then
            echo "${APP_NAME} is not running"
    else
            echo "stop serve.sh"
            echo "stop ${APP_NAME}"

            kill -9 ${XII}
            kill -9 ${PID}
    fi
    sleep 2
    ps -ef|grep ${APP_NAME}
}

restart() {
    stop
    sleep 2
    start
}

status() {
   PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
    if [[ -z "$PID" ]] ;then
            echo "${APP_NAME} is not running"
    else
            echo "${APP_NAME} is running, pid $PID"
    fi
    sleep 1
}

case $1 in
'start')
    start
    ;;
'stop')
    stop
    ;;
'restart')
    restart
    ;;
'status')
    status
    ;;
*)
    echo "Usage: $0 { start | stop | restart | status }"
    exit 2
    ;;
esac

